let initData = {
    arr: [], //请求回来得数据
    shopArr: [], // 购物车数据
    checkAll:false, // 用于控制购物车得全选与反选
    addressArr:[
        {
            name1:"赵只豪",
            address:"北京市昌平区",
            phone:"1234567"
        }
    ] ,// 放地址得数组,
    pullArr:[],
    pinglunArr:[]
}
let ShopReducer = (state = initData, action) => {
    let newArr = JSON.parse(JSON.stringify(state.arr)) // 不影响我原来定义得arr
    let newShopArr = JSON.parse(JSON.stringify(state.shopArr))
    let newaddressArr = JSON.parse(JSON.stringify(state.addressArr))
    let newPullArr = JSON.parse(JSON.stringify(state.pullArr))
    let newpinglunArr = JSON.parse(JSON.stringify(state.pinglunArr))
    if (action.type == "getList") {
        newArr = action.payload
        return {
            ...state,
            arr: newArr
        }
    } else if (action.type == "addshop") {
        console.log(state.shopArr, 5555);
        let findIndex = state.shopArr.findIndex((item) => {
            return item.id == action.payload.id
        })
        console.log(findIndex, 22222)

        if (findIndex !== -1) {
            console.log(2222);
            alert(("您已经加入购物车了"));
        } else {
            newShopArr.push(action.payload)
        }
        return {
            ...state,
            shopArr: newShopArr
        }
    } else if (action.type == "addnum") {
        let newShoparr1 = state.shopArr.map((item, index) => {
            if (item.id == action.payload) {
                item.number++
            }
            return item
        })
        return {
            ...state,
            shopArr: newShoparr1
        }


    } else if (action.type == "Del") {
        let newShoparr2 = state.shopArr.map((item, index) => {
            if (item.id == action.payload) {
                item.number--
                if (item.number < 0) {
                    item.number = 0
                    console.log("不能在减了")
                }
            }
            return item
        })
        return {
            ...state,
            shopArr: newShoparr2
        }
    } else if (action.type == "checkitem") {
        let newShoparr3 = state.shopArr.map((item,index)=>{
            if (item.id == action.payload) {
                item.checked = !item.checked
            }
            return item
        })
        let flagALL = state.shopArr.every((item,index)=>{
              return item.checked
        })
        return {
            ...state,
            shopArr:newShoparr3,
            checkAll:flagALL
        }

    }  else if(action.type == "checkall"){
        state.checkAll = !state.checkAll
        let flag =  state.checkAll
        let newShopArr5 = state.shopArr.map((item,index)=>{
            item.checked = flag
        })
        return {
            ...state,
            checkAll:flag,
            newShopArr:newShopArr5
        }
    } else if (action.type == "addAddress") {
        // console.log(newaddressArr,33333333,action.paylaod)
        newaddressArr.push(action.payload) // push返回得是新数组得长度
        console.log(action.payload,55555)
        console.log(newaddressArr,77777);
       
        return {
            ...state,
            addressArr:newaddressArr

        }
    } else if (action.type == "editAddress") {
           newaddressArr[action.ind] = action.payload
           return {
            ...state,
            addressArr:newaddressArr
        }
    } 
    else if (action.type == "delshan") {
        newaddressArr.splice(action.payload,1)
        return {
         ...state,
         addressArr:newaddressArr
     }
 } else if (action.type == "delshan2") {
    newaddressArr.splice(action.payload,1)
    return {
        ...state,
        addressArr:newaddressArr
    }
 }
 else if (action.type == "getPullList") {
    if (action.payload1 == "xiala") {
        newPullArr = action.payload
        return {
            ...state,
            pullArr:newPullArr
        }
    } else if (action.payload1 == "shangla") {
        console.log(action.payload,777777);

         newPullArr = [...state.pullArr,...action.payload]
       
         return {
            ...state,
            pullArr:newPullArr
         }
    } else {
        newPullArr = action.payload
        return {
            ...state,
            pullArr:newPullArr
        }
    }
   
 }
 else if (action.type == "getPinglunlist") { // 请求评论数据的
        newpinglunArr = action.payload
        return {
            ...state,
            pinglunArr:newpinglunArr
        }
 }

 else if (action.type == "addShoucang") { // 收藏加1
    let newpinglunArr1 = state.pinglunArr.map((item,index)=>{
        if (item.id == action.payload) {
            item.num ++
        }
        return item
    })
    return {
        ...state,
        pinglunArr:newpinglunArr1
    }
}


     else {
        return {
            ...state
        }
    }

}
export default ShopReducer